home *** CD-ROM | disk | FTP | other *** search
- /* Tangrams.h */
-
- #pragma mark Problem Specification
-
- typedef struct MyVertex {
- float h; /* horizontal coordinate of vertex */
- float v; /* vertical coordinate of vertex */
- } MyVertex;
-
- #define kMaxVertices 10
-
- typedef struct MyPolygon {
- long numVertices; /*the number of vertices*/
- MyVertex vertex[kMaxVertices]; /*the vertices*/
- } MyPolygon;
-
- typedef struct MyTransform {
- float flipH; /* If doFlip - flip polygon about this horiz.coord.*/
- float rotateClockwise; /* radians to rotate - */
- float rotateCenterH; /* - around this horiz coord - */
- float rotateCenterV; /* - and this vert coord */
- float translateH; /* translate this far horiz */
- float translateV; /* translate this far vert */
- Boolean doFlip; /* Flip if true */
- } MyTransform;
-
- void SolveTangram(
- MyPolygon *theShape, /* shape to be assembled */
- long numHoles, /* number of holes in theShape (>=0) */
- MyPolygon *theHoles[], /* holes in theShape (>=0) */
- long numPieces, /* number of pieces in theShape */
- MyPolygon *thePieces[], /* pieces in theShape */
- MyTransform *theXForms[] /* transform for each piece */
- );
-
- /* INCLUDES */
-
- #include <math.h>
-
- #define MyError(msg) ExitToShell();
-
- /* CLASSES */
-
- class TVector
- {
- /* A vector expressed in polar coordinates */
- friend class TList;
- protected:
- float angle; // clockwise radians
- float length; // length
- TVector *next;
- public:
- // Constructors
- TVector();
- TVector(TVector& v);
- TVector(float dir, float len);
- // Operators
- TVector& operator = (TVector& aVector);
- TVector& operator += (TVector& aVector);
- TVector& operator -= (TVector& aVector);
- Boolean operator == (TVector& aVector);
- Boolean operator > (TVector& aVector);
- Boolean operator < (TVector& aVector);
- Boolean operator || (TVector& aVector); /* parallel */
- // Access
- float getLength ();
- float getAngle ();
- TVector* getNext ();
- void set (float dir, float len);
- void setLength (float len);
- void setAngle (float dir);
- void setNext (TVector *aVector);
- // Transformation
- void rotate (float anAngle);
- void reverse ();
- void flip (float around);
- // Cartesian/Polar conversion
- void setHV (float h, float v);
- void setHV (MyVertex *pt);
- void getHV (MyVertex *pt);
- // Is Intercept of the line from p1 to p2 on h=0 < distance?
- friend Boolean Intercept (TVector *p1start, TVector *p1,
- TVector *p2start, TVector *p2);
- };
-
- class TList
- {
- /* A list of TVector instances */
- protected:
- TVector *start;
- TVector *end;
- public:
- TList();
- ~TList();
- void IList(TList& aList);
- void insert (TVector *aVector); /* add aVector to the end */
- void insert (TVector *aVector, TVector *afterVector);
- void remove (TVector *aVector);
- TVector* first (void); /* return first node */
- TVector* next (TVector *aVector);
- TVector* last (void); /* return last node */
- Boolean valid (TVector *aVector);
- };
-
- class TPolygon : public TVector
- {
- /* A Polygon can be rotated and translated.
- "Align" places the polygon with its first side along the first side of the
- input polygon
- "Rotate" turns the polygon through an angle about its origin
- "Translate" moves the polygon along a vector */
- protected:
- TList sides;
- /* A list of Vectors defining the direction and distance
- of each vertex from its predecessor.
- The Vector in the base class stores direction and
- distance from the origin to the start point. */
-
- public:
- /* Constructors */
- TPolygon ();
- TPolygon (TPolygon& p);
- void IPolygon(TPolygon *shape);
- void IPolygon(MyPolygon *shape);
- /* Access */
- TVector getOrigin (void);
- TVector* firstSide (void);
- TVector* nextSide (TVector *aSide);
- TVector* lastSide (void);
- float getRotation (void);
- /* Transformations */
- void rotate (float angle);
- void rotateTo (float direction);
- void translate (TVector *where);
- void translateTo (TVector *where);
-
- Boolean fill (TList *unplaced, TList *placed);
- Boolean contains (TPolygon *p);
- void align (TPolygon *thePiece);
- void subtract (TPolygon *aPiece);
- };
-
- class TShape : public TPolygon
- {
- /* A Shape is a Polygon used to fill a Tangram.
- It may be flipped and turned.
- It remembers its first defined side and flipped state
- "Flip" turns the shape over about the perpendicular
- bisector of its current first side
- "Turn" moves the shape so that its next side and vertex take
- the start point and direction of its current first side and vertex */
- protected:
- Boolean flipped;
- /* Is this Shape currently flipped? */
- TVector* firstDefinedSide;
- /* Stores a pointer to the first side */
- public:
- TShape();
- void IShape (MyPolygon *theData);
- virtual Boolean canFlip(void) {return false;}
- /* The base class cannot be flipped and always returns false */
- Boolean flip(void);
- /* Flip the Shape if canFlip()
- Return false if it is not now flipped. */
- Boolean turn ();
- /* Change the current first side.
- Return false if the next side is the firstDefinedSide */
- virtual void getTransform (MyTransform *theTransform[]);
- };
-
- class TPiece : public TShape
- {
- /* A Piece is a Shape that can really be flipped, and
- It remembers the index of the input data that defined it,
- and the direction and relative position of the first defined side for recovery
- of its transform into the MyTransform structure at that index */
- private:
- long index;
- TVector firstOrigin;
- TVector firstDirection;
- public:
- void IPiece (MyPolygon *theData[], long i);
- virtual Boolean canFlip(void) {return true;}
- virtual void getTransform (MyTransform *theTransform[]);
- };
-
- class TTangram : public TPolygon
- {
- /* The Tangram to solve is a Polygon that we want to fill with Shapes */
- protected:
- TList unplaced;
- TList placed;
- MyTransform **xForms;
-
- public:
- TTangram ();
- TTangram (TTangram& aShape);
- void ITangram (MyPolygon *theShape,
- long numHoles, MyPolygon *theHoles[],
- long numPieces, MyPolygon *thePieces[],
- MyTransform *xForms[]);
- void solve(void);
- };
-
-
-
-